package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.AlbumEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsAlbumActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GoodsAlbumModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private GoodsAlbumActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public GoodsAlbumModel(GoodsAlbumActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getSupplierPhotosByAlbumId(boolean isLoadMore,String id) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("page", page);
        map.get().put("limit", size);
        map.get().put("companyNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        map.get().put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfGoodsPublishAPI().getSupplierPhotosByAlbumId(id)
                .enqueue(new Callback<BaseEntity<List<AlbumEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<AlbumEntity>>> call, Response<BaseEntity<List<AlbumEntity>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showList(response.body().getData(), isLoadMore);
                        } else {
                            assert response.body() != null;
                            MToast.showToast(activity, response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<AlbumEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

}
